<?php

/**
 * Created by phpstorem.
 * User: yangakw@qq.com
 * Sign: 猥琐发育，别浪
 * Date: 2018-06-23
 * Time: 上午 10:20
 */
class CArticle extends CI_Controller
{

    public $user;

    function get_user(){
        $this->user = $this->session->userdata('user_info');
        if(empty($this->user) ){
            t_( "<script> location.href = '/login';</script>" );
            exit();
        }
    }

    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('session');

    }

    function manage(){
        $this->get_user();
        $this->load->view('article/base');
        $this->load->view('article/index');
    }

    function load_html_obj($url){
        $this->load->library('Simple_html_dom.php');
        $html = new simple_html_dom();

       do{
           set_error_handler(
               create_function(
                   '$severity, $message, $file, $line',
                   'throw new ErrorException($message, $severity, $severity, $file, $line);'
               )
           );
           try {
               $html->load_file($url);
           }
           catch (Exception $e) {
                exit('<h1>参数错误</h1>');
           }
           restore_error_handler();
       }while(0);

        $page_content = $html->find('html',0);
        if(empty($page_content)){
            $this->load->view('article/base');
            $this->load->view('article/error');
            die;
        }
        foreach($page_content->find("img") as &$img){
            $img->src = "/i?src=".$img->getAttribute("data-src");
        }
        foreach($page_content->find("script") as &$script){
            $script->outertext = '';
        }
        return $page_content;
    }
    function load_html($url){
        $key = sprintf("url_%s",base64_encode($url));
        $this->load->library('MQ');
        $html = MQ::hGet($key);
        if(!$html){
            $page_content = $this->load_html_obj($url);
            $html = $page_content->outertext;
            MQ::hSet($key,$html);
        }
        return $html;
    }
    function add(){
        $this->get_user();
        if($this->input->get('url')){
            $url = $this->input->get('url');
            $page_content = $this->load_html_obj($url);
            $title = $page_content->find('title',0);
            $title = $title->innertext;
            $source_url = $url;
            $user_id = isset($this->user->id)?$this->user->id:-1;
            $code = md5($url.$user_id);
            $data = [
                "user_id"=>$user_id,
                "code"=>$code,
                "title"=>$title,
                "source_url"=>$source_url,
//                "html"=>$this->load_html($source_url),
            ];
            $this->load->model('article');
            $this->article->save($data);
            $this->load->view('article/base');
            $this->load->view('article/success');
            return;
        }

        $this->load->view('article/base');
        $this->load->view('article/add');
    }


    function article_delete(){
        $id = $this->input->get('id');
        $this->load->model('article');
        $this->article->del_by_id($id);
        $this->load->view('article/base');
        $this->load->view('article/success');
    }

    function article_list(){
        $this->get_user();
        $user_id = isset($this->user->id)?$this->user->id:-1;
        $this->load->model('article');
        $articles = $this->article->by_user_id($user_id);
        $this->load->vars("articles",$articles);
        $this->load->view('article/base');
        $this->load->view('article/article_list');
    }

    function article_by_code($code = ""){
        $this->get_user();
        $this->load->model('article');
        $article = $this->article->by_code($code);
        if(empty($article)){
            return t_("没有内容");
        }
        $this->load->vars("article",$article);
        $html = $this->load_html($article->source_url);
        t_($html);
        $this->ad_logic();
    }


    /**
     * 广告
     */
    public function ad_logic($page_content=[]){
        $this->load->view('ad/ad_logic');
        $this->load->view('ad/ad_logic_1');
        $this->load->view('ad/ad_logic_2');
        $this->load->view('ad/ad_logic_3');
        $this->load->view('ad/ad_logic_4');
    }




}